<?php
namespace lib;

/**
 * 应用程序容器 (十分初级，简单的对象工厂)
 *
 * Class DataAccessFactory
 * @package lib\Data
 */
class Applicotion
{
	/**
     * @var Application
     */
    private static $instance = null;

    private $classBindings = [];
    private $instances = [];

    public function __construct()
    {
    	//
    }

    /**
     *  获取Appliation类的唯一实例 （单例模式）
     *
     * @return Application
     */
    public function getInstance()
    {
    	if (empty(self::$instance)) {
    		self::$instance = new Self();
    	}
    	return self::$instance;
    }

    /**
     * 将具体的实现类，或子类与父类(抽象类，接口)绑定
     *
     * @param $abstract
     * @param $implement
     */
    public function bind($abstract, $implement)
    {
    	$this->classBindings[$abstract] = $implement;
    }

    /**
     * 从容器(对象工厂)当中得到对象实例
     *
     * @param $type
     * @param $directory
     * @return JsonFileAccess|SerializeDataFileAccess
     */
    public function getObject($abstract)
    {
    	if (isset($this->classBindings[$abstract]) && !empty($this->classBindings[$abstract])) {
    		$emplements = $this->classBindings[$abstract];
    		$obj = $this->classBindings[$implements];
    		return $obj;
    	}
    	return null;
    }

    public function createObject($implementClass)
    {
    	if (class_exists($implementClass)) {
            $reflectionClass = new \ReflectionClass($implementClass);
            return $reflectionClass->newInstance();
        }
        return null;
    }
}